/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.activiti.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.FormService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.tompai.activiti.service.ActTaskService;
import com.tompai.activiti.vo.ProcessVO;
import com.tompai.activiti.vo.TaskVO;
import com.tompai.common.utils.PageUtils;

/**
 * @author: tompai
 * @email：liinux@qq.com
 * @createTime: 2019年11月24日 下午11:55:29
 * @history:
 * @version: v1.0
 */
@RequestMapping("activiti/task")
@RestController
public class TaskController {
	@Autowired
	RepositoryService repositoryService;
	@Autowired
	FormService formService;
	@Autowired
	TaskService taskService;
	@Autowired
	ActTaskService actTaskService;

	@GetMapping("goto")
	public ModelAndView gotoTask() {
		return new ModelAndView("act/task/gotoTask");
	}

	@GetMapping("/gotoList")
	PageUtils list(int offset, int limit) {
		List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery().listPage(offset,
				limit);
		int count = (int) repositoryService.createProcessDefinitionQuery().count();
		List<Object> list = new ArrayList<>();
		for (ProcessDefinition processDefinition : processDefinitions) {
			list.add(new ProcessVO(processDefinition));
		}

		PageUtils pageUtils = new PageUtils(list, count);
		return pageUtils;
	}

	@GetMapping("/form/{procDefId}")
	public void startForm(@PathVariable("procDefId") String procDefId, HttpServletResponse response)
			throws IOException {
		String formKey = actTaskService.getFormKey(procDefId, null);
		response.sendRedirect(formKey);
	}

	@GetMapping("/form/{procDefId}/{taskId}")
	public void form(@PathVariable("procDefId") String procDefId, @PathVariable("taskId") String taskId,
			HttpServletResponse response) throws IOException {
		// 获取流程XML上的表单KEY

		String formKey = actTaskService.getFormKey(procDefId, taskId);

		response.sendRedirect(formKey + "/" + taskId);
	}

	@GetMapping("/todo")
	ModelAndView todo() {
		return new ModelAndView("act/task/todoTask");
	}

	@GetMapping("/todoList")
	List<TaskVO> todoList() {
		List<Task> tasks = taskService.createTaskQuery().taskAssignee("admin").list();
		List<TaskVO> taskVOS = new ArrayList<>();
		for (Task task : tasks) {
			TaskVO taskVO = new TaskVO(task);
			taskVOS.add(taskVO);
		}
		return taskVOS;
	}

	/**
	 * 读取带跟踪的图片
	 */
	@RequestMapping(value = "/trace/photo/{procDefId}/{execId}")
	public void tracePhoto(@PathVariable("procDefId") String procDefId, @PathVariable("execId") String execId,
			HttpServletResponse response) throws Exception {
		InputStream imageStream = actTaskService.tracePhoto(procDefId, execId);

		// 输出资源内容到相应对象
		byte[] b = new byte[1024];
		int len;
		while ((len = imageStream.read(b, 0, 1024)) != -1) {
			response.getOutputStream().write(b, 0, len);
		}
	}

}
